rm(list = ls(all=TRUE))
Step 1: Clearing Environment.
library (xts)
library (zoo)
library (PerformanceAnalytics)
require (sandwich)
require(lmtest)
library(dplyr)
library(tidyquant)
library(quantmod)
library (quantreg)
library(pastecs)
require (tvReg)
library (brms)
library (MSwM)
library (quantreg)
Step 2: Loading all the required libraries
options("getSymbols.warning4.0"=TRUE)
options("getSymbols.yahoo.warning"=TRUE)
getSymbols(c("AAPL","MSFT","GOOG","AMZN","NVDA","META","TSLA","AVGO","ORCL","CRM", "CSCO","ADBE","AMD", "TXN", "NFLX"), from = '2015-01-01',
to = "2023-01-01",warnings = FALSE,
auto.assign = TRUE, )
## [1] "AAPL" "MSFT" "GOOG" "AMZN" "NVDA" "META" "TSLA" "AVGO" "ORCL" "CRM"
## [11] "CSCO" "ADBE" "AMD" "TXN" "NFLX"
stock_names <- c("Apple Inc.", "Microsoft Corporation", "Alphabet Inc.", "Amazon.com, Inc.", "NVIDIA Corporation", "Meta Platforms, Inc.", "Tesla, Inc.", "Broadcom Inc.", "Oracle Corporation", "Salesforce.com, Inc.", "Cisco Systems, Inc.", "Adobe Inc.", "Advanced Micro Devices, Inc.", "Texas Instruments Incorporated", "Netflix, Inc.")
Step 3: Market of Interest: Tech Market in US
Reason: As we are trying to measure herding over here and it is known that herding can be due to many reasons. A few are listed below
Now retail investors (Individuals doing trading themself) have all the above points relevant to them and hence they are more prone to herding behaviour. The reason to give this explanation was that the percentage of these retail investors is on the rise in recent years and their favourite industry is the Tech industry. (https://www.nasdaq.com/articles/the-10-most-popular-recurring-investments-of-retail-investors)
Hence, the 15 stocks with the highest market cap in tech industry in US were selected.
The code above gets the data for the stocks from Yahoo finance from 2015-01-01 to 2023-01-01.
# Create data frame with daily closing prices
stock_close <- data.frame(AAPL$AAPL.Close, MSFT$MSFT.Close, GOOG$GOOG.Close, AMZN$AMZN.Close, NVDA$NVDA.Close, META$META.Close, TSLA$TSLA.Close, AVGO$AVGO.Close, ORCL$ORCL.Close, CRM$CRM.Close, CSCO$CSCO.Close, ADBE$ADBE.Close, AMD$AMD.Close, TXN$TXN.Close, NFLX$NFLX.Close)
# Convert data frame to xts object
stock_xts <- xts(stock_close, order.by = as.Date(row.names(stock_close)))
Step 4: The close prices of all the stocks are stored in a dataframe named stock_close and then it is converted into a timeseries object.
##chart series
chart_Series(stock_xts$AAPL.Close, name = stock_names[1])
chart_Series(stock_xts$MSFT.Close, name = stock_names[2])
chart_Series(stock_xts$GOOG.Close, name = stock_names[3])
chart_Series(stock_xts$AMZN.Close, name = stock_names[4])
chart_Series(stock_xts$NVDA.Close, name = stock_names[5])
chart_Series(stock_xts$META.Close, name = stock_names[6])
chart_Series(stock_xts$TSLA.Close, name = stock_names[7])
chart_Series(stock_xts$AVGO.Close, name = stock_names[8])
chart_Series(stock_xts$ORCL.Close, name = stock_names[9])
chart_Series(stock_xts$CRM.Close, name = stock_names[10])
chart_Series(stock_xts$CSCO.Close, name = stock_names[11])
chart_Series(stock_xts$ADBE.Close, name = stock_names[12])
chart_Series(stock_xts$AMD.Close, name = stock_names[13])
chart_Series(stock_xts$TXN.Close, name = stock_names[14])
chart_Series(stock_xts$NFLX.Close, name = stock_names[15])
Step 5: The code below gives the price charts of all the stocks. If we take a glimpse of the charts we can observe that all individually they have different movements but overall the trend has been similar. This is due to the fact that they are from the same industry and secondly some external factors like:
##TAKING LOG RETURNS
return = Return.calculate( stock_xts , method = "log") # automatically calculate return
return
## AAPL.Close MSFT.Close GOOG.Close AMZN.Close NVDA.Close
## 2015-01-02 NA NA NA NA NA
## 2015-01-05 -2.857604e-02 -0.009238362 -0.021065954 -0.020730694 -0.0170343921
## 2015-01-06 9.413332e-05 -0.014786098 -0.023449905 -0.023098053 -0.0307874680
## 2015-01-07 1.392479e-02 0.012625288 -0.001714703 0.010543957 -0.0026089837
## 2015-01-08 3.770251e-02 0.028993730 0.003148076 0.006812759 0.0369273352
## 2015-01-09 1.071932e-03 -0.008440682 -0.013035137 -0.011818170 0.0040201020
## 2015-01-12 -2.494934e-02 -0.012581468 -0.007322685 -0.018765212 -0.0126168715
## 2015-01-13 8.839546e-03 -0.005163477 0.007342807 0.011362407 -0.0015248128
## 2015-01-14 -3.817823e-03 -0.008665599 0.009407829 -0.004999968 0.0040609155
## 2015-01-15 -2.751538e-02 -0.010498774 0.001835103 -0.021785701 -0.0071174367
## ...
## 2022-12-16 -1.468611e-02 -0.017500921 -0.003734997 -0.006692741 -0.0227316260
## 2022-12-19 -1.603751e-02 -0.017479958 -0.018999506 -0.034035030 -0.0193152291
## 2022-12-20 -5.289007e-04 0.005598796 0.005369693 0.003174469 -0.0104517924
## 2022-12-21 2.353045e-02 0.010817989 0.006893542 0.018376816 0.0255337532
## 2022-12-22 -2.405975e-02 -0.025860255 -0.022296569 -0.034947222 -0.0730223511
## 2022-12-23 -2.802040e-03 0.002264504 0.017409272 0.017274437 -0.0087085300
## 2022-12-27 -1.397557e-02 -0.007441809 -0.021155254 -0.026265686 -0.0740269721
## 2022-12-28 -3.116586e-02 -0.010307873 -0.016859178 -0.014800722 -0.0060376366
## 2022-12-29 2.793062e-02 0.027254897 0.028392511 0.028435658 0.0396015166
## 2022-12-30 2.465841e-03 -0.004949731 -0.002476291 -0.002140568 0.0007529905
## META.Close TSLA.Close AVGO.Close ORCL.Close CRM.Close
## 2015-01-02 NA NA NA NA NA
## 2015-01-05 -0.016191494 -0.042950329 -0.016114746 -0.0168339203 -0.0182272913
## 2015-01-06 -0.013564847 0.005648256 -0.023006025 -0.0103771429 -0.0168158147
## 2015-01-07 0.000000000 -0.001563152 0.026654561 0.0002318261 -0.0047314634
## 2015-01-08 0.026308765 -0.001565531 0.048766102 0.0060073729 0.0287415878
## 2015-01-09 -0.005643966 -0.018980627 0.010637894 -0.0004608401 -0.0073662056
## 2015-01-12 -0.013207451 -0.021768131 -0.016726313 -0.0027694209 -0.0074208695
## 2015-01-13 -0.003525554 0.010037949 -0.005638217 -0.0078886755 0.0041486953
## 2015-01-14 -0.002226128 -0.058262085 0.007865240 0.0030236345 -0.0124980140
## 2015-01-15 -0.029670183 -0.004264609 -0.020224418 -0.0100362680 -0.0380964435
## ...
## 2022-12-16 0.027847956 -0.048336805 -0.003752600 -0.0061384203 -0.0167759190
## 2022-12-19 -0.042330264 -0.002399206 -0.011361029 0.0114943564 0.0057524275
## 2022-12-20 0.022542674 -0.083964838 -0.010259287 0.0054509852 -0.0043501784
## 2022-12-21 0.022546919 -0.001670449 0.031144722 0.0068948506 0.0142998078
## 2022-12-22 -0.022290684 -0.093023119 -0.014609983 -0.0124700937 -0.0085552997
## 2022-12-23 0.007824484 -0.017706675 -0.001194070 0.0044626406 0.0019332643
## 2022-12-27 -0.009875814 -0.121138258 0.002007262 0.0016066849 0.0093810853
## 2022-12-28 -0.010838769 0.032553261 -0.015750011 -0.0080590791 -0.0169031339
## 2022-12-29 0.039347108 0.077726403 0.023434432 0.0132321188 0.0311890125
## 2022-12-30 0.000664956 0.011102160 0.002363615 0.0041681602 0.0003771965
## CSCO.Close ADBE.Close AMD.Close TXN.Close NFLX.Close
## 2015-01-02 NA NA NA NA NA
## 2015-01-05 -0.0201214450 -0.004988827 -0.0037523459 -0.0156414761 -0.0522379710
## 2015-01-06 -0.0003696259 -0.020350215 -0.0113422654 -0.0166623168 -0.0172687988
## 2015-01-07 0.0091996972 0.008189849 -0.0191945203 0.0178012227 0.0051784172
## 2015-01-08 0.0076629090 0.025134942 0.0115608118 0.0161840307 0.0219456234
## 2015-01-09 0.0101266934 -0.014921552 0.0076337086 0.0005598354 -0.0155784643
## 2015-01-12 0.0093123281 -0.010072743 0.0000000000 -0.0089955545 -0.0322807739
## 2015-01-13 0.0017809851 -0.007480190 0.0113422654 -0.0062316889 0.0154371331
## 2015-01-14 -0.0064263093 -0.008536095 -0.0113422654 -0.0015166227 0.0013888154
## 2015-01-15 -0.0184353759 0.004277210 -0.0427249957 -0.0060894517 -0.0014814332
## ...
## 2022-12-16 -0.0070863186 0.029466402 -0.0169777474 -0.0027093400 0.0010324471
## 2022-12-19 -0.0086125905 -0.029314248 -0.0126156646 -0.0115080308 -0.0083246150
## 2022-12-20 -0.0006331665 0.028368544 0.0070967085 -0.0091100677 -0.0003815690
## 2022-12-21 0.0061033741 0.009299666 0.0396344679 0.0157699908 0.0333392273
## 2022-12-22 -0.0071594358 -0.014338691 -0.0580975230 -0.0241741602 -0.0007050124
## 2022-12-23 0.0033755274 0.005718857 0.0102820044 -0.0019447682 -0.0094144833
## 2022-12-27 0.0010525048 -0.009977265 -0.0195639108 -0.0036567940 -0.0372670271
## 2022-12-28 -0.0097252160 -0.020379982 -0.0111253653 -0.0159402478 -0.0259884809
## 2022-12-29 0.0090938621 0.027783320 0.0353282679 0.0234828960 0.0501513196
## 2022-12-30 0.0029430206 -0.003115185 -0.0007717116 0.0012112220 0.0128329739
Step 6: Then we calculate the log returns for easier calculation and interpretability.
descriptive.stat.return = stat.desc(return) # descriptive statistics
descriptive.stat.return
Step 7: Descriptive Statistics
# a function to create CSAD and Rm
exchange.herd = function(return)
{
n=ncol(return)
Rm = rowMeans(return)
temp_dif =abs(return-Rm)
temp_sum = rowSums(temp_dif)
CSAD = temp_sum / ncol(return)
CSAD = cbind (CSAD, Rm)
return (CSAD)
}
Step 8: Calculating CSAD and Rm
A function is developed which helps us to calculate the CSAD and Rm. Where CSAD is the cross sectional absolute deviation of the stock relative to the equally weighted market portfolio and Rm is the market return.
Through the relationship between CSAD and Rm, we can calculate herding.
###CALLING THE FUNCTION
f = exchange.herd(return) # calling the function "exchange.herd" that calculates CSAD and Rm
head (f)
## CSAD Rm
## [1,] NA NA
## [2,] 0.008576786 -0.020247019
## [3,] 0.007539064 -0.014409079
## [4,] 0.008581977 0.004969119
## [5,] 0.012294762 0.020288735
## [6,] 0.008348518 -0.004146365
Step 9: Applying the function created above on return (log returns of stocks)
##DATA PROCESSING
CSAD.df = fortify.zoo(f)
CSAD.df$Rm2 = CSAD.df$Rm^2
Step 10: In this step, f (Zoo object which has the CSAD and Rm values), is converted into a dataframe.
Secondly in the newly created dataframe CSAD.df, another column Rm2 is added which is the square of Rm (market returns)
## ADDING UP/DOWN days BINARY COLUMN
CSAD.df$D_up <- ifelse(CSAD.df$Rm > 0, 1, 0)
CSAD.df = CSAD.df[-c(1),] # removing the first row with NAs
head (CSAD.df)
tail (CSAD.df)
Step 11: In this step, firstly another column D_up is added in the CSAD.df dataframe, which has a value of 1 when Rm > 0 otherwise it has a value of 0. This is done for the calculations of herding in Up/Down days.
Then we remove the first row from CSAD.df, as it had NA values because of the log transformation of data that we did above.
##DATA PROCESSING
dates <- seq(as.Date("2015-01-01"), by = "day", length.out = nrow(CSAD.df))
rownames(CSAD.df) <- dates
CSAD.df <-CSAD.df[,-1]
CSAD.df
Step 12: The above code is simply to get data in the form we want for the further process. It simply adds the dates as index and removes the numerical index which was appearing in the data frame CSAD.df.
##DEFINING VARIABLES FOR ALL DAYS HERDING
y = CSAD.df$CSAD
x1 = abs (CSAD.df$Rm)
x2 = CSAD.df$Rm2
Step 13: DEFINING VARIABLES FOR (Unconditional) ALL DAYS HERDING ANALYSIS
In this part, we have defined y as CSAD and x1 and x2 as per the equation for the overall assessment of herding in data. x1 = absolute of market return x2 = Rm2 (Square of market return)
### SETTING VARIABLES FOR UP/DOWN DAYS
y_ud = CSAD.df$CSAD
x1_ud = abs(CSAD.df$Rm)*CSAD.df$D_up
x2_ud = abs(CSAD.df$Rm)*(1-CSAD.df$D_up)
x3_ud = CSAD.df$Rm2*CSAD.df$D_up
x4_ud = CSAD.df$Rm2*(1-CSAD.df$D_up)
Step 14:
Defining Variables For (Conditional) UP/DOWN DAYS DATA ANALYSIS
This step is done to create variables as per the UP/DOWN market days herding analysis equation. y_ud is kept as CSAD.
x1_ud = absolute of Rm * D_up x2_ud = absolute of Rm * (1-D_up) x3_ud = Rm2 * D_up x4_ud = Rm2 * (1-D_up)
where, Rm2 is square of market return (Rm) and D_up are binary numbers explaining if Rm was greater than 0 or not.
#LINEAR REGRESSION MODEL FOR ALL DAYS
linearMod <- lm(y~x1+x2) # build linear regression model on full data
print(linearMod)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Coefficients:
## (Intercept) x1 x2
## 0.008271 0.231846 -0.507627
summary(linearMod)
##
## Call:
## lm(formula = y ~ x1 + x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.012643 -0.002968 -0.000798 0.001858 0.043236
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0082706 0.0001693 48.838 <2e-16 ***
## x1 0.2318455 0.0158022 14.672 <2e-16 ***
## x2 -0.5076267 0.2387981 -2.126 0.0336 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.004598 on 2010 degrees of freedom
## Multiple R-squared: 0.2222, Adjusted R-squared: 0.2214
## F-statistic: 287.1 on 2 and 2010 DF, p-value: < 2.2e-16
Step 15:
LINEAR MODEL FOR ALL DAYS
It can be seen in the above Linear Regression analysis that the p value is significant for both x1 and x2 are both significant.
Over here we use the linear regression model to assess herding. Although the very significant positive value of x1 hints towards no herding, but the x2 which is also significant according to 95% confidence interval (Which is also strong) hints towards unconditional herding.
The p-value is more significant for x1 but x2 is also significant and the coefficient is more negative (-0.5) as compared to x2 (0.23). This gives a mixed result, hence more tests can be done.
#Newey-West Heteroscedasticity and Autocorrelation consistent (HAC) estimators FOR ALL DAYS
coeftest(linearMod,vcov=NeweyWest(linearMod,verbose=T))
##
## Lag truncation parameter chosen: 9
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.00827056 0.00018719 44.1823 < 2e-16 ***
## x1 0.23184551 0.01785823 12.9826 < 2e-16 ***
## x2 -0.50762669 0.20712491 -2.4508 0.01434 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Step 16:
NEWEY-WEST REGRESSION FOR ALL DAYS.
This tests with an assumption of heteroscedasticity and autocorrelation in the data. It uses linear regression model but is a bit different from it.
Result: The result is almost the same as the Linear regression, but the significance of p value of x2 has increase a bit, increasing the possibility of herding to be present.
##TIME VARYING MODEL of Regression FOR ALL DAYS
tvlm.fit = tvLM(y~x1+x2, bw = NULL )
## Calculating regression bandwidth... bw = 0.08345362
head (tvlm.fit$coefficients)
## (Intercept) x1 x2
## [1,] 0.007534182 0.3501151 0.19385476
## [2,] 0.007529756 0.3517268 0.10801711
## [3,] 0.007525207 0.3534320 0.01709678
## [4,] 0.007520510 0.3552389 -0.07930978
## [5,] 0.007515649 0.3571526 -0.18146989
## [6,] 0.007510617 0.3591753 -0.28950135
plot(tvlm.fit$coefficients[,1], type="l", ylim=c(-6, 10),
xlab="Index", ylab="Coefficient Value")
lines(tvlm.fit$coefficients[,2], type="l", col="red")
lines(tvlm.fit$coefficients[,3], type="l", col="blue")
legend("topleft", legend=c("intercept", "x1", "x2"),
col=c("black", "red", "blue"), lty=1)
Step 17:
TIME VARYING REGRESSION FOR ALL DAYS
This model basically explains the herding period over time.
On the y-axis Index 0 over here is our starting date 2015-01-02. Each unit in index represents a day. The last value on the y-axis is the last date till which the data is present (2023-01-01).
Results: So we can see that strong herding behaviour is present in the between the first quarter of 2015 and second to third quarter of 2016. (Index 400 to 600)
## COMPARING HERDING ON HIGH AND LOW VOLATILITY DAYS
chart_Series(stock_xts$AAPL.Close, name = stock_names[1])
chart_Series(stock_xts$MSFT.Close, name = stock_names[2])
chart_Series(stock_xts$GOOG.Close, name = stock_names[3])
chart_Series(stock_xts$AMZN.Close, name = stock_names[4])
chart_Series(stock_xts$NVDA.Close, name = stock_names[5])
STEP 18: COMPARING HERDING RESULTS WITH HIGH/ LOW VOLATILITY DAYS
To see if HIGH VOLATILITY or LOW VOLATILITY in the market is more prone to herding, we have plotted 5 of the stocks out of the 15 we have in this project to see the high and low volatility zones. It can be seen that overall the movement of all the stock prices is similar.
Now lets compare volatility and Herding
As given in the Time varying analysis above we have herding in the first quarter of 2015 and second to third quarter of 2016. If we see the prices on the above charts we can see that these are periods of very low volatility. In the high periods we don’t observe a strong herding behaviour as per the Time Varying Regression.
Result: Hence, we can conclude that for our selected stocks, Herding behaviours are strong in a market which low on volatility.
###BAYESIAN MODEL FOR ALL DAYS
hourly = cbind(y, x1, x2)
model = brm(formula = y ~ x1+x2,
data = hourly,
seed = 123)
## Compiling Stan program...
## Trying to compile a simple C file
## Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
## using C compiler: ‘Apple clang version 14.0.0 (clang-1400.0.29.102)’
## using SDK: ‘MacOSX12.3.sdk’
## clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:88:
## /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:16: error: expected ';' after top level declarator
## namespace Eigen {
## ^
## ;
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
## /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
## ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
## Start sampling
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 1.8e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.18 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.659352 seconds (Warm-up)
## Chain 1: 0.534118 seconds (Sampling)
## Chain 1: 1.19347 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 1.1e-05 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
## Chain 2: Adjust your expectations accordingly!
## Chain 2:
## Chain 2:
## Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 2:
## Chain 2: Elapsed Time: 0.638826 seconds (Warm-up)
## Chain 2: 0.520351 seconds (Sampling)
## Chain 2: 1.15918 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 1.1e-05 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
## Chain 3: Adjust your expectations accordingly!
## Chain 3:
## Chain 3:
## Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 3:
## Chain 3: Elapsed Time: 0.644187 seconds (Warm-up)
## Chain 3: 0.510198 seconds (Sampling)
## Chain 3: 1.15438 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 1.1e-05 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
## Chain 4: Adjust your expectations accordingly!
## Chain 4:
## Chain 4:
## Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 4:
## Chain 4: Elapsed Time: 0.473317 seconds (Warm-up)
## Chain 4: 0.541993 seconds (Sampling)
## Chain 4: 1.01531 seconds (Total)
## Chain 4:
summary(model)
## Family: gaussian
## Links: mu = identity; sigma = identity
## Formula: y ~ x1 + x2
## Data: hourly (Number of observations: 2013)
## Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
## total post-warmup draws = 4000
##
## Population-Level Effects:
## Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept 0.01 0.00 0.01 0.01 1.00 1934 2445
## x1 0.23 0.02 0.20 0.26 1.00 1296 1238
## x2 -0.51 0.24 -0.99 -0.06 1.01 1275 1424
##
## Family Specific Parameters:
## Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma 0.00 0.00 0.00 0.00 1.00 1598 1592
##
## Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
Step 19:
BAYESIAN MODEL FOR ALL DAYS
It can be seen in the Bayesian model that the coefficient for x1 and x2 has the same sign and magnitude as the above models. The rhat value of 1 indicates the algorithm is converged.
### MARKOV-REGIME SWITCHING MODEL FOR ALL DAYS
nstates <- 2 # a number of states
msEuro = msmFit(linearMod, k = nstates, sw = rep(TRUE, 4)) # estimation; linearMod is an object from a linear estimation
summary(msEuro) #show the
## Markov Switching Model
##
## Call: msmFit(object = linearMod, k = nstates, sw = rep(TRUE, 4))
##
## AIC BIC logLik
## -16674.47 -16595.18 8343.233
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.0117 0.0005 23.4000 < 2.2e-16 ***
## x1(S) 0.3158 0.0446 7.0807 1.434e-12 ***
## x2(S) -1.1125 0.4572 -2.4333 0.01496 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.005693417
## Multiple R-squared: 0.2759
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.0076146606 -0.0017809130 -0.0011454485 -0.0007298262 0.0386527821
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.0074 0.0001 74.0000 < 2.2e-16 ***
## x1(S) 0.1706 0.0147 11.6054 < 2.2e-16 ***
## x2(S) -0.7347 0.2709 -2.7121 0.006686 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.002555541
## Multiple R-squared: 0.2547
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.0073113578 -0.0013555477 0.0002990065 0.0017382909 0.0049011141
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.6624563 0.1234917
## Regime 2 0.3375437 0.8765083
plotProb(msEuro, which = 1) # create the plot
Step 20: MARKOW - REGIME SWITCHING MODEL FOR ALL DAYS
This model uses an Expectation Maximization algorithm, it allows changes in behaviour of variables on the basis of some regimes. Results: x2 is negative and significant in both the Regimes, with a 99% confidence interval in Regime 1 and 95% confidence interval in Regime 2. This shows a very strong herding behaviour.
In the transition probabilities it can be observed that Regime 1 is more persistent in shifting to another regime with a probability of 87.6% as compared to the probability of 66.24% for Regime 2. These transition probabilities for a regime basically tell us the probability of the state of the Regime to changing to some other state. These regimes can basically be depicting high volaitility/low volatility or bearish or bullish markets.
##QUANTILE ANALYSIS FOR ALL DAYS
taus<-seq(from = .1, to = .9, by = .1)
coef0 <- rq( y ~ x1+x2, tau=taus)
summary (coef0)
## Warning in summary.rq(xi, U = U, ...): 2 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.1
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00478 0.00016 30.72617 0.00000
## x1 0.14032 0.02764 5.07680 0.00000
## x2 -0.43965 0.63654 -0.69069 0.48984
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.2
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00547 0.00016 34.70241 0.00000
## x1 0.17360 0.02776 6.25369 0.00000
## x2 -0.63508 0.76142 -0.83407 0.40434
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.3
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00628 0.00017 37.04008 0.00000
## x1 0.16414 0.02360 6.95471 0.00000
## x2 0.06323 0.55681 0.11356 0.90959
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.4
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00700 0.00016 43.04166 0.00000
## x1 0.17541 0.01877 9.34550 0.00000
## x2 0.09201 0.36818 0.24990 0.80269
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.5
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00788 0.00018 42.63986 0.00000
## x1 0.18259 0.02282 8.00224 0.00000
## x2 0.00106 0.44169 0.00240 0.99808
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.6
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00857 0.00018 46.42329 0.00000
## x1 0.20043 0.02887 6.94270 0.00000
## x2 -0.15407 0.65243 -0.23614 0.81335
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.7
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00937 0.00025 37.91312 0.00000
## x1 0.23604 0.03813 6.19039 0.00000
## x2 -0.43661 0.85477 -0.51079 0.60955
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.8
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.01065 0.00032 33.50351 0.00000
## x1 0.27175 0.05243 5.18307 0.00000
## x2 -0.10032 1.32361 -0.07580 0.93959
##
## Call: rq(formula = y ~ x1 + x2, tau = taus)
##
## tau: [1] 0.9
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.01289 0.00038 34.29680 0.00000
## x1 0.35402 0.04789 7.39221 0.00000
## x2 -1.32556 0.68188 -1.94398 0.05204
coef0
## Call:
## rq(formula = y ~ x1 + x2, tau = taus)
##
## Coefficients:
## tau= 0.1 tau= 0.2 tau= 0.3 tau= 0.4 tau= 0.5
## (Intercept) 0.004779842 0.005469355 0.006276203 0.007001666 0.007883250
## x1 0.140322145 0.173600490 0.164135873 0.175412096 0.182590481
## x2 -0.439652265 -0.635082415 0.063233734 0.092009213 0.001061857
## tau= 0.6 tau= 0.7 tau= 0.8 tau= 0.9
## (Intercept) 0.008572586 0.009374959 0.01064744 0.01288631
## x1 0.200430310 0.236037288 0.27174708 0.35402258
## x2 -0.154065997 -0.436613068 -0.10032315 -1.32556026
##
## Degrees of freedom: 2013 total; 2010 residual
plot(coef0, type ="l")
Step 21:
Quantile Regression Analysis for All Days
If we analyse the significant x2 values in the quantiles, we observe a stronger herding behaviour in the higher quantiles of return variation, specifically 0.9.
##LINEAR REGRESSION ANALYSIS FOR UP/DOWN DAYS
linearMod_ud <- lm(y_ud~x1_ud+x2_ud+x3_ud+x4_ud) # build linear regression model on full data
print(linearMod_ud)
##
## Call:
## lm(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud)
##
## Coefficients:
## (Intercept) x1_ud x2_ud x3_ud x4_ud
## 0.008283 0.232079 0.223724 -0.271889 -0.512002
summary(linearMod_ud)
##
## Call:
## lm(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.013494 -0.002982 -0.000821 0.001893 0.043165
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.0082832 0.0001738 47.647 <2e-16 ***
## x1_ud 0.2320787 0.0220174 10.541 <2e-16 ***
## x2_ud 0.2237235 0.0175965 12.714 <2e-16 ***
## x3_ud -0.2718889 0.4386911 -0.620 0.5355
## x4_ud -0.5120017 0.2628178 -1.948 0.0515 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.004597 on 2008 degrees of freedom
## Multiple R-squared: 0.223, Adjusted R-squared: 0.2215
## F-statistic: 144.1 on 4 and 2008 DF, p-value: < 2.2e-16
Step 22:
LINEAR MODEL FOR UP/DOWN DAYS.
Over here we can see that the x3 is not significant and x4 is significant with 90% confidence interval and the coefficient is negative (-0.512).
This means: Herding on UP market days on average is not present, but Herding on Down market days on average is present. In simple, words people tend to herd and follow others more when the market is diving down.
#Up/Down Newey-West Heteroscedasticity and Autocorrelation consistent (HAC) estimators FOR UP/DOWN DAYS
coeftest(linearMod_ud,vcov=NeweyWest(linearMod_ud,verbose=T))
##
## Lag truncation parameter chosen: 8
##
## t test of coefficients:
##
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.00828318 0.00018683 44.3349 < 2e-16 ***
## x1_ud 0.23207873 0.02594639 8.9445 < 2e-16 ***
## x2_ud 0.22372355 0.02047882 10.9246 < 2e-16 ***
## x3_ud -0.27188889 0.65258369 -0.4166 0.67699
## x4_ud -0.51200172 0.19341311 -2.6472 0.00818 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Step 23: NEWEY-WEST REGRESSION FOR UP/DOWN Days.
This tests with an assumption of heteroscedasticity and autocorrelation in the data. It uses linear regression model but is a bit different from it.
Result: The result is almost the same as the Linear regression, but the significance of p value of x4 has increase a bit, showing higher probability of herding on down days.
# Up/Down estimate TV Linear Regression
tvlm.fit_ud = tvLM(y_ud~x1_ud+x2_ud+x3_ud+x4_ud, bw = NULL )
## Calculating regression bandwidth... bw = 0.1195414
head (tvlm.fit_ud$coefficients)
## (Intercept) x1_ud x2_ud x3_ud x4_ud
## [1,] 0.007625838 0.4835959 0.2806926 -4.041075 -2.662048
## [2,] 0.007631309 0.4842999 0.2800986 -4.127412 -2.650723
## [3,] 0.007637000 0.4849228 0.2794817 -4.209944 -2.638947
## [4,] 0.007642904 0.4854677 0.2788426 -4.288825 -2.626740
## [5,] 0.007649013 0.4859374 0.2781821 -4.364199 -2.614118
## [6,] 0.007655320 0.4863346 0.2775009 -4.436201 -2.601097
plot(tvlm.fit_ud$coefficients[,1], type="l", ylim=c(-6, 10),
xlab="Index", ylab="Coefficient Value")
lines(tvlm.fit_ud$coefficients[,2], type="l", col="red")
lines(tvlm.fit_ud$coefficients[,3], type="l", col="blue")
lines(tvlm.fit_ud$coefficients[,4], type="l", col="green")
lines(tvlm.fit_ud$coefficients[,5], type="l", col="orange")
legend("topleft", legend=c("intercept", "x1_ud", "x2_ud","x3_ud","x4_ud"),
col=c("black", "red", "blue","green","orange"), lty=1,x.intersp=1, y.intersp=1, bty="n", ncol=3, cex=0.8)
Step 24: TIME VARYING REGRESSION FOR UP/DOWN Days
This model basically explains the herding period over time.
On the y-axis Index 0 over here is our starting date 2015-01-02. Each unit in index represents a day. The last value on the y-axis is the last date till which the data is present (2023-01-01).
Results: We can observe that initially in the early months of 2015 (Index 0-100), herding in Up days dominates as the x3 is more negative, but first quarter of 2016 (Index 400), things start to change and the herding in Down days dominates as x4 becomes more negative.
# Up/Down Bayesian models
hourly_ud = cbind(y_ud, x1_ud, x2_ud,x3_ud,x4_ud)
model_ud = brm(formula = y_ud ~ x1_ud+x2_ud+x3_ud+x4_ud,
data = hourly_ud,
seed = 123)
## Compiling Stan program...
## recompiling to avoid crashing R session
## Trying to compile a simple C file
## Running /Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB foo.c
## using C compiler: ‘Apple clang version 14.0.0 (clang-1400.0.29.102)’
## using SDK: ‘MacOSX12.3.sdk’
## clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/Rcpp/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/unsupported" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/BH/include" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/src/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppParallel/include/" -I"/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/rstan/include" -DEIGEN_NO_DEBUG -DBOOST_DISABLE_ASSERTS -DBOOST_PENDING_INTEGER_LOG2_HPP -DSTAN_THREADS -DBOOST_NO_AUTO_PTR -include '/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp' -D_REENTRANT -DRCPP_PARALLEL_USE_TBB=1 -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c foo.c -o foo.o
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:88:
## /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:1: error: unknown type name 'namespace'
## namespace Eigen {
## ^
## /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/src/Core/util/Macros.h:628:16: error: expected ';' after top level declarator
## namespace Eigen {
## ^
## ;
## In file included from <built-in>:1:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/StanHeaders/include/stan/math/prim/mat/fun/Eigen.hpp:13:
## In file included from /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Dense:1:
## /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/RcppEigen/include/Eigen/Core:96:10: fatal error: 'complex' file not found
## #include <complex>
## ^~~~~~~~~
## 3 errors generated.
## make: *** [foo.o] Error 1
## Start sampling
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 1).
## Chain 1:
## Chain 1: Gradient evaluation took 1.4e-05 seconds
## Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0.14 seconds.
## Chain 1: Adjust your expectations accordingly!
## Chain 1:
## Chain 1:
## Chain 1: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 1: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 1: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 1: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 1: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 1: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 1: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 1: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 1: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 1: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 1: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 1: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 1:
## Chain 1: Elapsed Time: 0.898423 seconds (Warm-up)
## Chain 1: 0.595389 seconds (Sampling)
## Chain 1: 1.49381 seconds (Total)
## Chain 1:
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 2).
## Chain 2:
## Chain 2: Gradient evaluation took 7e-06 seconds
## Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0.07 seconds.
## Chain 2: Adjust your expectations accordingly!
## Chain 2:
## Chain 2:
## Chain 2: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 2: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 2: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 2: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 2: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 2: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 2: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 2: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 2: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 2: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 2: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 2: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 2:
## Chain 2: Elapsed Time: 0.907138 seconds (Warm-up)
## Chain 2: 0.667347 seconds (Sampling)
## Chain 2: 1.57449 seconds (Total)
## Chain 2:
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 3).
## Chain 3:
## Chain 3: Gradient evaluation took 8e-06 seconds
## Chain 3: 1000 transitions using 10 leapfrog steps per transition would take 0.08 seconds.
## Chain 3: Adjust your expectations accordingly!
## Chain 3:
## Chain 3:
## Chain 3: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 3: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 3: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 3: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 3: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 3: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 3: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 3: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 3: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 3: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 3: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 3: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 3:
## Chain 3: Elapsed Time: 1.02993 seconds (Warm-up)
## Chain 3: 0.591197 seconds (Sampling)
## Chain 3: 1.62113 seconds (Total)
## Chain 3:
##
## SAMPLING FOR MODEL 'bf5a07990f5449695fca8f9b87af9436' NOW (CHAIN 4).
## Chain 4:
## Chain 4: Gradient evaluation took 1.1e-05 seconds
## Chain 4: 1000 transitions using 10 leapfrog steps per transition would take 0.11 seconds.
## Chain 4: Adjust your expectations accordingly!
## Chain 4:
## Chain 4:
## Chain 4: Iteration: 1 / 2000 [ 0%] (Warmup)
## Chain 4: Iteration: 200 / 2000 [ 10%] (Warmup)
## Chain 4: Iteration: 400 / 2000 [ 20%] (Warmup)
## Chain 4: Iteration: 600 / 2000 [ 30%] (Warmup)
## Chain 4: Iteration: 800 / 2000 [ 40%] (Warmup)
## Chain 4: Iteration: 1000 / 2000 [ 50%] (Warmup)
## Chain 4: Iteration: 1001 / 2000 [ 50%] (Sampling)
## Chain 4: Iteration: 1200 / 2000 [ 60%] (Sampling)
## Chain 4: Iteration: 1400 / 2000 [ 70%] (Sampling)
## Chain 4: Iteration: 1600 / 2000 [ 80%] (Sampling)
## Chain 4: Iteration: 1800 / 2000 [ 90%] (Sampling)
## Chain 4: Iteration: 2000 / 2000 [100%] (Sampling)
## Chain 4:
## Chain 4: Elapsed Time: 0.93786 seconds (Warm-up)
## Chain 4: 0.59728 seconds (Sampling)
## Chain 4: 1.53514 seconds (Total)
## Chain 4:
summary(model_ud)
## Family: gaussian
## Links: mu = identity; sigma = identity
## Formula: y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud
## Data: hourly_ud (Number of observations: 2013)
## Draws: 4 chains, each with iter = 2000; warmup = 1000; thin = 1;
## total post-warmup draws = 4000
##
## Population-Level Effects:
## Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## Intercept 0.01 0.00 0.01 0.01 1.00 2041 2857
## x1_ud 0.23 0.02 0.19 0.27 1.00 1351 1861
## x2_ud 0.22 0.02 0.19 0.26 1.00 1750 2041
## x3_ud -0.27 0.44 -1.14 0.60 1.00 1467 1935
## x4_ud -0.51 0.26 -1.03 -0.01 1.00 1873 2366
##
## Family Specific Parameters:
## Estimate Est.Error l-95% CI u-95% CI Rhat Bulk_ESS Tail_ESS
## sigma 0.00 0.00 0.00 0.00 1.00 2091 2017
##
## Draws were sampled using sampling(NUTS). For each parameter, Bulk_ESS
## and Tail_ESS are effective sample size measures, and Rhat is the potential
## scale reduction factor on split chains (at convergence, Rhat = 1).
Step 25:
BAYESIAN MODEL FOR UP/DOWN DAYS
The magnitude of the coefficient is approximately the same as for the regression models, hence it just strengthens the conclusion that the herding is strong on Down days. The rhat value of 1 indicates the algorithm is converged.
##MARKOV REGIME SWITCHING MODEL FOR UP/DOWN DAYS
nstates <- 2 # a number of states
msEuro_ud = msmFit(linearMod_ud, k = nstates, sw = rep(TRUE, 6)) # estimation; linearMod is an object from a linear estimation
summary(msEuro_ud) #show the
## Markov Switching Model
##
## Call: msmFit(object = linearMod_ud, k = nstates, sw = rep(TRUE, 6))
##
## AIC BIC logLik
## -16670.36 -16538.22 8345.182
##
## Coefficients:
##
## Regime 1
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.0114 0.0005 22.8000 < 2.2e-16 ***
## x1_ud(S) 0.3437 0.0551 6.2377 4.441e-10 ***
## x2_ud(S) 0.3936 0.0560 7.0286 2.086e-12 ***
## x3_ud(S) -1.4012 0.7872 -1.7800 0.0750760 .
## x4_ud(S) -3.1336 0.8155 -3.8426 0.0001217 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.005665305
## Multiple R-squared: 0.2686
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.0076514416 -0.0018272476 -0.0011879340 -0.0007493702 0.0385690302
##
## Regime 2
## ---------
## Estimate Std. Error t value Pr(>|t|)
## (Intercept)(S) 0.0074 0.0001 74.0000 < 2e-16 ***
## x1_ud(S) 0.1842 0.0197 9.3503 < 2e-16 ***
## x2_ud(S) 0.1269 0.0137 9.2628 < 2e-16 ***
## x3_ud(S) -1.1414 0.4621 -2.4700 0.01351 *
## x4_ud(S) 0.4072 0.1702 2.3925 0.01673 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.002528294
## Multiple R-squared: 0.2972
##
## Standardized Residuals:
## Min Q1 Med Q3 Max
## -0.0067289970 -0.0013363108 0.0003044222 0.0017463949 0.0047859076
##
## Transition probabilities:
## Regime 1 Regime 2
## Regime 1 0.6443297 0.1309326
## Regime 2 0.3556703 0.8690674
plotProb(msEuro_ud, which = 1) # create the plot
Step 26: MARKOW- REGIME SWITCHING MODEL FOR UP/DOWN DAYS
In the regime 1, the x4 is has twice as magnitude as x3 and is more significant, hence it hints also at strong herding on the Down days.
In the regime 2, the significant levels are the same and x3 has a negative coefficients, so it shows that when the market is in Regime 2 situation, it has herding on the UP Days.
The transition probabilities show that Regime 2 has a higher probability of transitioning to other state/regime (86.9%) as compared to Regime 1 which has a transition probability of (64.4%)
#UP/Down Quantile regression
taus<-seq(from = .1, to = .9, by = .1)
coef0_ud <- rq(y_ud ~ x1_ud+x2_ud+x3_ud+x4_ud, tau=taus)
summary (coef0_ud)
## Warning in summary.rq(xi, U = U, ...): 2 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
## Warning in summary.rq(xi, U = U, ...): 1 non-positive fis
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.1
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00476 0.00020 24.32405 0.00000
## x1_ud 0.14947 0.02582 5.78892 0.00000
## x2_ud 0.12495 0.07040 1.77480 0.07608
## x3_ud -0.47364 0.38527 -1.22938 0.21907
## x4_ud -0.30069 2.05735 -0.14615 0.88381
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.2
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00549 0.00018 30.96764 0.00000
## x1_ud 0.17865 0.02514 7.10689 0.00000
## x2_ud 0.15906 0.04937 3.22154 0.00130
## x3_ud -0.75903 0.62950 -1.20576 0.22805
## x4_ud -0.32755 1.58712 -0.20638 0.83651
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.3
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00623 0.00017 37.54892 0.00000
## x1_ud 0.18447 0.02660 6.93529 0.00000
## x2_ud 0.15507 0.02529 6.13076 0.00000
## x3_ud -0.50424 0.76556 -0.65865 0.51019
## x4_ud 0.26809 0.58579 0.45766 0.64724
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.4
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00697 0.00017 41.35698 0.00000
## x1_ud 0.18408 0.03164 5.81810 0.00000
## x2_ud 0.17006 0.01708 9.95432 0.00000
## x3_ud 0.08748 1.01739 0.08599 0.93148
## x4_ud 0.13012 0.24070 0.54058 0.58886
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.5
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00778 0.00019 40.65276 0.00000
## x1_ud 0.20636 0.03582 5.76108 0.00000
## x2_ud 0.17355 0.02715 6.39274 0.00000
## x3_ud -0.24940 1.16068 -0.21488 0.82989
## x4_ud 0.06784 0.56198 0.12071 0.90393
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.6
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00863 0.00021 40.74300 0.00000
## x1_ud 0.17462 0.04291 4.06957 0.00005
## x2_ud 0.17951 0.04141 4.33483 0.00002
## x3_ud 1.26044 1.54270 0.81703 0.41401
## x4_ud -0.01312 0.99864 -0.01314 0.98952
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.7
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.00953 0.00023 41.24275 0.00000
## x1_ud 0.19881 0.03131 6.34979 0.00000
## x2_ud 0.22549 0.04402 5.12187 0.00000
## x3_ud 0.91033 0.55113 1.65176 0.09874
## x4_ud -0.37140 1.03629 -0.35839 0.72009
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.8
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.01067 0.00031 34.15550 0.00000
## x1_ud 0.27099 0.04272 6.34329 0.00000
## x2_ud 0.26215 0.07538 3.47769 0.00052
## x3_ud 0.03333 0.61830 0.05390 0.95702
## x4_ud -0.67698 1.94269 -0.34847 0.72752
##
## Call: rq(formula = y_ud ~ x1_ud + x2_ud + x3_ud + x4_ud, tau = taus)
##
## tau: [1] 0.9
##
## Coefficients:
## Value Std. Error t value Pr(>|t|)
## (Intercept) 0.01279 0.00044 28.92274 0.00000
## x1_ud 0.37811 0.06422 5.88821 0.00000
## x2_ud 0.34245 0.11048 3.09948 0.00197
## x3_ud -1.31561 1.27370 -1.03291 0.30177
## x4_ud -1.32879 2.88220 -0.46104 0.64482
plot(coef0_ud, type = "l")
Step 27:
QUANTILE REGRESSION FOR UP/DOWN DAYS
If we focus on the significant values of x3 and x4 over here, most of the values are insignificant. The only significant x value is x3 when tau is 0.7, which also is positive showing no signs of herding in Up days.
But if we just focus on the coefficients and the plots we can see that the coefficients of x4 are negative for tau 0.6 to 0.9, indicating some herding on DOWN DAYS.
Similarly in the first quantile although the p values are not signficant but coefficients are strongly negative which might indicate a possibility of herding in both Up and Down days.
STEP 28: CONCLUSION
For LM,Newey West, BAYESIAN model for herding on normal days we got mixed results with the coefficient x1 being positive and more significant and indicating no herding whereas the x2 coefficient was negative with more magniture of coefficient but the significance was less.
For UP/DOWN days we have seen that there are indications of herding in Down market days as compared to Up days.
Quantile results in both types mostly have insignificant p values. When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Cmd+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.